package com.acwing.partition3;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/2/11 19:36
 */
public class AC240食物链 {

    private static final int N = 50010;
    private static int[] parent = new int[N], dist = new int[N];

    private static int n = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        //设 dist[x] 表示 x 与 parent[x] 的关系，0 代表是同类，1 代表是x吃parent[x], 根据关系图自然2就代表x被parent[x]吃。
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        int k = Integer.parseInt(ss[1]), ans = 0;
        for (int i = 0; i <= n; i++) parent[i] = i;
        for (int i = 0; i < k; i++) {
            ss = reader.readLine().split(" ");
            int t = Integer.parseInt(ss[0]), a = Integer.parseInt(ss[1]), b = Integer.parseInt(ss[2]);
            if (a > n || b > n) ans++;
            else {
                int pa = find(a), pb = find(b);
                if (t == 1) {
                    //应该是同类，如果查询出来的权值不等，说明当前话是假话
                    if (pa == pb && dist[a] % 3 != dist[b] % 3) ans++;
                    else if (pa != pb) {
                        dist[pa] = ((dist[b] - dist[a]) + 3) % 3;
                        parent[pa] = pb;
                    }
                } else {
                    //a应该是吃b的
                    if (pa == pb && (dist[a] + 1) % 3 != dist[b] % 3) ans++;
                    else if (pa != pb) {
                        dist[pa] = ((dist[b] - dist[a] - 1) + 3) % 3;
                        parent[pa] = pb;
                    }
                }
            }
        }
        writer.write(ans + "\n");
        writer.flush();
    }

    private static int find(int x) {
        if (x != parent[x]) {
            int root = find(parent[x]);
            dist[x] = (dist[x] + dist[parent[x]]) % 3;
            parent[x] = root;
        }
        return parent[x];
    }
}
